/**
 * Created by c.y on 2018/6/14
 * 文件说明: 文件步骤信息字段的check与提交数据的整合（对于订单详情）
 */

import validate from '../tool/validator';

/**
 * 检查单个步骤的所有字段是否必填
 * @param singleStepProdAttrs 单个步骤的所有字段数组
 * @param context 组件的作用域(用于提示语)
 * @return {boolean} 是否通过验证
 */
function checkStepProdAttrs(singleStepProdAttrs, context) {
    let len = singleStepProdAttrs.length;
    let singleCheckResult = true;
    if (singleStepProdAttrs instanceof Array && singleStepProdAttrs.length) {
        for (let i = 0; i < len; i++) {
            // 如果该字段是必填的话，那么必须判断是否必填
            if (singleStepProdAttrs[i].isRequired) {
                // 如果该字段是手机
                if (singleStepProdAttrs[i].typeId === 2) {
                    if (validate.checkValEmpty(singleStepProdAttrs[i].attrValues) || validate.checkValEmpty(singleStepProdAttrs[i].attrValues[0].strValue)) {
                        context.$Message.error('请输入' + singleStepProdAttrs[i].attrKey);
                        singleCheckResult = false;
                        return false;
                    } else if (!validate.checkPhone(singleStepProdAttrs[i].attrValues[0].strValue)) {
                        context.$Message.error('请输入正确的手机号');
                        singleCheckResult = false;
                        return false;
                    }
                    // 如果该字段是整数
                } else if (singleStepProdAttrs[i].typeId === 3) {
                    if (validate.checkValEmpty(singleStepProdAttrs[i].attrValues[0].intValue) || validate.checkValEmpty(singleStepProdAttrs[i].attrValues)) {
                        context.$Message.error('请输入' + singleStepProdAttrs[i].attrKey);
                        singleCheckResult = false;
                        return false;
                    } else if (!validate.checkCustomNumberField(singleStepProdAttrs[i].attrValues[0].intValue)) {
                        context.$Message.error('请输入正确的' + singleStepProdAttrs[i].attrKey);
                        singleCheckResult = false;
                        return false;
                    }
                    // 该字段是其他类型
                } else if (singleStepProdAttrs[i].typeId !== 3 && (validate.checkValEmpty(singleStepProdAttrs[i].attrValues) ||
                    validate.checkValEmpty(singleStepProdAttrs[i].attrValues[0].strValue))) {
                    context.$Message.error('请输入' + singleStepProdAttrs[i].attrKey);
                    singleCheckResult = false;
                    return false;
                }
                // 如果该字段是非必填的话，用户填写的话，那么需要验证组件
            } else {
                if (singleStepProdAttrs[i].typeId === 2 && !validate.checkValEmpty(singleStepProdAttrs[i].attrValues[0].strValue)) {
                    if (!validate.checkPhone(singleStepProdAttrs[i].attrValues[0].strValue)) {
                        context.$Message.error('请输入正确的手机号');
                        singleCheckResult = false;
                        return false;
                    }
                    // 如果该字段是整数
                } else if (singleStepProdAttrs[i].typeId === 3 && !validate.checkValEmpty(singleStepProdAttrs[i].attrValues[0].intValue)) {
                    if (!validate.checkCustomNumberField(singleStepProdAttrs[i].attrValues[0].intValue)) {
                        context.$Message.error('请输入正确的' + singleStepProdAttrs[i].attrKey);
                        singleCheckResult = false;
                        return false;
                    }
                }
            }
        }
    }
    return singleCheckResult;
}


/**
 * 检查所有步骤的是否必填
 * 检查的数据格式checkProdAttrs [ {prodAttrs：[]}, {prodAttrs：[]}]
 * @param allStepProdAttrs 所有步骤的字段列表
 * @param content 当前作用域
 * @return {boolean}
 */
function checkAllStepAttrs (allStepProdAttrs, content) {
    let checkAllResult = true;
    if (allStepProdAttrs instanceof Array && allStepProdAttrs.length) {
        for (let i = 0; i < allStepProdAttrs.length; i++) {
            if(!checkStepProdAttrs(allStepProdAttrs[i], content)) {
                checkAllResult = false;
                return false;
            }
        }
    }
    return checkAllResult;
}


/**
 * 组合所有步骤提交数据，满足后台的更新数据的要求
 * @param allStepProdAttrs 所有步骤的字段列表
 * @param orderDetailInfo 订单详情
 */
function combFormStepAttr (allStepProdAttrs, orderDetailInfo) {
    let submitInfo = [];
    let stepIdList = [];
    let attrKeysList = [];
    // 对于stepId的组合
    if (orderDetailInfo instanceof Array && orderDetailInfo.length) {
        orderDetailInfo.forEach(function (item) {
            stepIdList.push(item.id);
        });
    }
    // 对于属性的组合
    if (allStepProdAttrs instanceof Array && allStepProdAttrs.length) {
        allStepProdAttrs.forEach(function (singleStep, singleIndex) {
            attrKeysList[singleIndex] = [];
            if (singleStep instanceof Array && singleStep.length) {
                singleStep.forEach(function (item) {
                    attrKeysList[singleIndex].push({
                        id: item.id,
                        typeId: item.typeId,
                        attrValues: item.attrValues
                    });
                });
            }
        });
    }
    // 整合数据
    if (stepIdList instanceof Array && stepIdList.length) {
        stepIdList.forEach(function (item, index) {
            submitInfo.push({
                stepId: item,
                attrKeys: attrKeysList[index]
            })
        });
    }
    return submitInfo;
}



export default {
    checkAllStepAttrs,
    combFormStepAttr
};
